# -*- coding:utf-8 -*-
#处理商品的数据
import requests
from pymongo import MongoClient
import time
from product.reviews import reviews
from product.custom import custom
import product.reviews
from product.mongodb import mongodb


conn = MongoClient(mongodb.ip, 27017)
db = conn.pinduoduo
batchDb = db.batch
goodsDb = db.goods
goodsDetailDb = db.goodsDetail
goodsSkuDb = db.goodsSku
headers = {
    'User-Agent':'android Mozilla/5.0 (Linux; Android 8.1.0; vivo NEX A Build/OPM1.171019.019; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36  phh_android_version/3.23.0 phh_android_build/228842 phh_android_channel/qihu360'
    #'accesstoken':'B4GKVGELNC5HEAUC244DPZNHADAESBFI3WM5KFXDSMIKAJBWBG4A1007e2d'
}
urls = [
    'http://apiv3.yangkeduo.com/search?q={0}&requery=0&page={1}&size={2}&pdduid=',
    'https://api.yangkeduo.com/api/oakstc/v15/goods/{0}'
]
ip={
    'http':'http://61.138.33.20:808',
    'https':'https://119.122.213.14:9000'
}

class goods:
    def selectByQuery(self,query,page,size,goodsTypeId):
        if(page < 1):
            page = 1
        if(size < 0):
            size = 50
        req = requests.get(url=urls[0].format(query,page,size),headers=headers).json()
        '''
        batchDb.insert({
            "batchId": (int(time.time())),
            "batchName":"根据条件获取商品",
            "batchUrl":urls[0].format(query,page,size),
            "batchCount":req['total']
        })
        '''
        #插入商品数据
        for data in req['items']:
            goodsDb.insert({
                "goods_id":str(data['goods_id']),
                'goods_type_id':1,
                "goods_name":data['goods_name'],
                "sales":str(data['sales']),
                "price":str(data['price']),
                "normal_price":str(data['normal_price']),
                "market_price":str(data['market_price']),
                "link_url":'https://mobile.yangkeduo.com/goods.html?goods_id='+str(data['goods_id']),
            })
            #处理详情
            detailReq = requests.get(url=urls[1].format(data['goods_id']),headers=headers).json()
            if ('error_code' not in detailReq):
                #处理隐形眼镜
                good = custom.custom()
                good.getData(detailReq,goodsTypeId)
                goodsDetailDb.insert({
                    'goods_sn': detailReq['goods_sn'],
                    'goods_id': detailReq['goods_id'],
                    'goods_type_id': 1,
                    'goods_name': detailReq['goods_name'],
                    'goods_desc': detailReq['goods_desc'],
                    'thumb_url': detailReq['thumb_url'],
                    'hd_thumb_url': detailReq['hd_thumb_url'],
                    'image_url': detailReq['image_url'],
                    'min_on_sale_group_price': str(detailReq['min_on_sale_group_price']),
                    'max_on_sale_group_price': str(detailReq['max_on_sale_group_price']),
                    'min_on_sale_normal_price': str(detailReq['min_on_sale_normal_price']),
                    'max_on_sale_normal_price': str(detailReq['max_on_sale_normal_price']),
                    'min_group_price': str(detailReq['min_group_price']),
                    'max_group_price': str(detailReq['max_group_price']),
                    'max_normal_price': str(detailReq['max_normal_price']),
                    'min_normal_price': str(detailReq['min_normal_price']),
                    'old_min_on_sale_group_price': str(detailReq['old_min_on_sale_group_price']),
                    'old_max_on_sale_group_price': str(detailReq['old_max_on_sale_group_price']),
                    'old_min_group_price': str(detailReq['old_min_group_price']),
                    'old_max_group_price': str(detailReq['old_max_group_price']),
                    'sales': str(detailReq['sales'])
                })
                for sku in detailReq['sku']:
                    specsStr = ''
                    for specses in sku['specs']:
                        specsStr += "{0}|{1}".format(specses['spec_key'], specses['spec_value'])
                    # 插入套餐
                    goodsSkuDb.insert({
                        'sku_id': sku['sku_id'],
                        'goods_id': sku['goods_id'],
                        'thumb_url': sku['thumb_url'],
                        'quantity': sku['quantity'],
                        'limit_quantity': sku['limit_quantity'],
                        'sold_quantity': sku['sold_quantity'],
                        'init_quantity': sku['init_quantity'],
                        'spec': sku['spec'],
                        'normal_price': str(sku['normal_price']),
                        'group_price': str(sku['group_price']),
                        'old_group_price': str(sku['old_group_price']),
                        'price': str(sku['price']),
                        'market_price': str(sku['market_price']),
                        'specs': specsStr
                    })